<!--
 @description
 @fileName index.vue
 @author zengqiongying
 @created 2022/07/27 09:56:28
-->
<template>
  <div class="my-page" :style="{'padding-top':topHeight+'px'}">
    <Tabbar @refresh="refreshPage" />
    <div ref="top" class="my-header">
      <MineHeader show-statusbar bg-color="transparent" :arrow="false" />
      <div class="my-user-info">
        <div class="my-avatar">
          <van-image :src="userInfo.photo" round width="100%" height="100%" lazy-load:="lazyLoad" show-loading fit="fill">
            <template v-slot:loading>
              <van-image :src="defualtAvatar" width="100%" />
            </template>
            <template v-slot:error>
              <van-image :src="defualtAvatar" width="100%" />
            </template>
          </van-image>
        </div>
        <div class="my-user-content">
          <div class="u-name">
            <span>{{ userInfo.userName }} </span>
            <span @click="toPage('/my/setting')">
              <img class="u-setting" src="~@/assets/my/icon_mine_setting.png">
            </span>
          </div>
          <div class="u-customer">
            <div class="c-name">{{ customer.name }}</div>
            <div class="btn-change" @click="toPage('/my/business?code='+customer.code)">
              <svg-icon icon-class="zuoyouqiehuan" />
              <span>切换</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-for="(g,i) in entryGroup" :key="i" class="menu-group-box">
      <template v-if="g.personalCenterEntryDTOList && g.personalCenterEntryDTOList.length>0">
        <div class="g-title">{{ g.name }}</div>
        <div class="g-menu-list">
          <div v-for="(menu,j) in g.personalCenterEntryDTOList" :key="j" class="menu-item" @click="goPage(menu)">
            <div class="image">
              <img :src="menu.iconPicUrl | filterImageUrl">
            </div>
            <div class="name">{{ menu.name }}</div>
          </div>
        </div>
      </template>
    </div>

    <!-- <div ref="list" class="my-nav-list">
      <div class="item-nav" @click="toPage('/my/order')">
        <img class="icon" src="~@/assets/my/icon_mine_order.png">
        <div class="txt">查看订单</div>
        <div class="arrow">
          <img src="~@/assets/my/ic_arrow.png">
        </div>
      </div>
      <div class="item-nav">
        <img class="icon" src="~@/assets/my/icon_mine_compalin.png">
        <div class="txt">当前版本</div>
        <div class="arrow">
          当前版本V3.0.0
        </div>
      </div>
      <div class="item-nav" @click="toPage('/my/setting')">
        <img class="icon" src="~@/assets/my/icon_mine_setting.png">
        <div class="txt">设置</div>
        <div class="arrow">
          <img src="~@/assets/my/ic_arrow.png">
        </div>
      </div>
    </div> -->
  </div>
</template>

<script>
import MineHeader from '@/components/MineHeader'
import Tabbar from '@/components/Tabbar'
import defualtAvatar from '@/assets/defaultAvatar.png'
import { getCurrentCustomer } from '@/apis/customerSelector'
import { queryPersonalCenterEntry } from '@/apis/index'
import { formatURL } from '@/utils'
export default {
  name: 'MyCenter',
  components: {
    MineHeader,
    Tabbar
  },
  filters: {
    filterImageUrl(url) {
      return formatURL(url, process.env.VUE_APP_STATIC)
    }
  },
  data() {
    return {
      topHeight: 175, // 顶部的高度
      defualtAvatar,
      customer: {
        code: '', // 经销商编码
        name: '' // 经销商名称
      }, // 经销商数据
      entryGroup: []
    }
  },

  computed: {
    userInfo() {
      return this.$store.getters.userInfo
    }
  },

  created() {
    this.getInitData()
  },
  activated() {
    this.getInitData()
  },

  mounted() {
    this.$nextTick(() => {
      setTimeout(() => {
        const topHeight = this.$refs.top.offsetHeight
        this.topHeight = topHeight
      }, 100)
    })
  },

  methods: {
    refreshPage(name) {
      if (this.$route.name === name) {
        this.getInitData()
      }
    },
    goPage(data) {
      data.routerTitle = data.name
      this.$gotoPage(data, this.$router)
    },
    toPage(path) {
      this.$router.push({ path })
    },
    getInitData() {
      // 获取当前经销商
      getCurrentCustomer().then(res => {
        this.customer = res.data || ''
      })
      // 查询当前入口配置
      queryPersonalCenterEntry().then(res => {
        console.log(res)
        this.entryGroup = res.data || []
      })
    }
  }
}

</script>
<style lang='less' scoped>
.my-page{
  padding-bottom: 50px;
  min-height: calc(100vh);
  box-sizing: border-box;
  background: #fff;
  .my-header{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 99;
    padding-bottom: 34px;
    background:#fff url(~@/assets/my/bg_mine.png) no-repeat center;
    background-size:  100% auto;
    box-sizing: border-box;
    /deep/ .header{
      background-color: transparent;
    }
    .my-user-info{
      margin-top: 7px;
      display: flex;
      align-items: center;
      padding: 0 20px;
      .my-avatar{
        position: relative;
        width: 64px;
        height: 64px;
        background: #E2FAFA;
        border-radius: 50%;
        margin-right: 16px;
      }
      .my-user-content{
        flex: 1;
        .u-name{
          height: 30px;
          line-height: 30px;
          font-size: 22px;
          font-weight: 600;
          color: #343640;
          display: flex;
          justify-content: space-between;
          justify-items: center;
        }
        .u-setting{
          font-weight: normal;
          font-size: 14px;
          line-height: 0;
          width: 24px;
          height: 24px;
        }
        .u-customer{
          margin-top: 6px;
          line-height: 20px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: 14px;
          color: #979AA9;
          .c-name{
            flex: 1;
            line-height: 1.2;
          }
          .btn-change{
            margin-left: 10px;
            width: 54px;
            height: 20px;
            border-radius: 10px;
            border: 1px solid rgba(151,154,169,0.3100);
            line-height: 18px;
            text-align: center;
            font-size:10px;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            .svg-icon{
              font-size: 16px;
            }
          }
        }
      }
    }
  }
  .my-nav-list{
    padding: 46px 20px 20px;
    .item-nav{
      margin-bottom: 36px;
      height: 22px;
      display: flex;
      align-items: center;
      .txt{
        flex: 1;
        line-height: 22px;
        font-size: 16px;
        font-weight: 500;
        color: #343640;
      }
      .icon{
        width: 20px;
        height: 20px;
        margin-right: 16px;
      }
      .arrow{
        margin-left: 16px;
        color: #B5B7C4;
        font-size: 14px;
        img{
          width: 16px;
          height: 16px;
        }
      }
    }
  }
}
.menu-group-box{
  padding: 12px 16px;
  margin-bottom:24px;
  .g-title{
    color: #343640;
    font-size: 14px;
    font-weight: bold;
    line-height: 20px;
    margin-bottom: 10px;
    padding: 0 16px;
  }
  .g-menu-list{
    display: flex;
    flex-wrap: wrap;
    .menu-item{
      width: 25%;
      padding: 0 15px;
      box-sizing: border-box;
      .image{
        position: relative;
        width: 100%;
        padding-bottom: 100%;
        overflow: hidden;
      }
      img{
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
      }
      .name{
        margin-top: 4px;
        height: 18px;
        line-height: 18px;
        font-size: 12px;
        color: #343640;
        text-align: center;
      }
    }
  }
}
</style>
